package com.martin.dms.web.services.impl;

import java.util.ArrayList;
import java.util.List;

import com.martin.dms.model.Item;
import com.martin.dms.model.Supplier;
import com.martin.dms.services.DeliveryService;
import com.martin.dms.services.ItemService;
import com.martin.dms.services.SupplierService;
import com.martin.dms.web.beans.ItemBean;
import com.martin.dms.web.beans.SupplierBean;
import com.martin.dms.web.beans.SupplierManagementBean;
import com.martin.dms.web.services.SupplierFlowService;

/**
 * Implementation of the SupplierFlowService.
 * 
 * @author mtsvetkov
 */
public class SupplierFlowServiceImpl implements SupplierFlowService {

    private SupplierService supplierService;
    private ItemService itemService;
    private DeliveryService deliveryService;

    /**
     * Constructor.
     * 
     * @param supplierService - service for supplier management.
     * @param itemService - service for item management.
     * @param deliveryService - service for delivery management.
     */
    public SupplierFlowServiceImpl(SupplierService supplierService, ItemService itemService,
            DeliveryService deliveryService) {
        this.supplierService = supplierService;
        this.itemService = itemService;
        this.deliveryService = deliveryService;
    }

    /**
     * {@inheritDoc}
     */
    public List<SupplierBean> searchSuppliers(SupplierManagementBean supplierManagementBean, boolean searchForSuppliers) {
        if (searchForSuppliers) {
            List<Supplier> suppliers = supplierService.findByNameEmailAndSuppliedItemName(supplierManagementBean
                    .getSupplierNameFilter(), supplierManagementBean.getSupplierEmailFilter(), supplierManagementBean
                    .getSuppliedItemNameFilter());
            List<SupplierBean> result = new ArrayList<SupplierBean>();
            for (Supplier supplier : suppliers) {
                result.add(new SupplierBean(supplier.getId(), supplier.getName()));
            }
            return result;
        }
        return new ArrayList<SupplierBean>();
    }

    /**
     * {@inheritDoc}
     */
    public SupplierBean getSupplier(long id, boolean copyAllData) {
        Supplier supplier = supplierService.get(id);
        if (copyAllData) {
            return new SupplierBean(supplier);
        } else {
            return new SupplierBean(supplier.getId(), supplier.getName());
        }
    }

    /**
     * {@inheritDoc}
     */
    public void deleteSupplier(SupplierBean supplier) {
        supplierService.delete(supplier.getId());
    }

    /**
     * {@inheritDoc}
     */
    public List<ItemBean> searchForItems(SupplierBean supplierBean, boolean searchForItems) {
        if (searchForItems) {
            List<Item> items = itemService.findByName(supplierBean.getItemsNameFilter());
            List<ItemBean> result = new ArrayList<ItemBean>();
            for (Item item : items) {
                result.add(new ItemBean(item.getId(), item.getName()));
            }
            result.removeAll(supplierBean.getItems());
            return result;
        }
        return new ArrayList<ItemBean>();
    }

    /**
     * {@inheritDoc}
     */
    public void saveSupplier(SupplierBean supplierBean) {
        if (supplierBean.getId() != null) {
            supplierService.update(supplierBean.getId(), supplierBean.getName(), supplierBean.getEmail(), supplierBean
                    .getAddress(), supplierBean.getPhoneNumber(), supplierBean.getItemIds());
        } else {
            supplierService.create(supplierBean.getName(), supplierBean.getEmail(), supplierBean.getAddress(),
                    supplierBean.getPhoneNumber(), supplierBean.getItemIds());
        }

    }

    /**
     * {@inheritDoc}
     */
    public Boolean makeDelivery(SupplierBean supplierBean) {
        deliveryService.makeDelivery(supplierBean.getId(), supplierBean.getItemIds().get(0), supplierBean
                .getIntDeliveryAmount());
        return true;
    }

}
